﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Text;
using System.IO;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace StudioLib
{

    // Global TODO List
    //
    // 1- RoomView :(
    // 2- Shedules - no edit      
    // 3- UI design - make it more .... nice.
    //dc 4- WCF service    - done, not tested
    // 5- Studio.Attributes - make it usable. 
    // 6- Key Check Algo - the whole liciensing is not finished yet.
    // 7- Correct saving/loading to/from .std. Make some file format instead of serialization.
    [Flags, Serializable]
    public enum DrumParts
    {
        Solo = 1,
        Pedal,
        Cardan,
        FreePlates
    }

    
    [Serializable]
    public class StudioAttribute
    {
        public string Name;
        public Type ValueType;

        public StudioAttribute(string name, Type type)
        {
            Name = name;
            ValueType = type;
        }
    }
    

    [Serializable]
    public class ReserveTable
    {
        [Serializable]
        public class ReserveUnit
        {
            public enum ReserveType
            {
                Free,
                Reserved,
                Uncertain
            }

            public ReserveType type;
            public string bandName;
        }

        public ReserveUnit[][] table;
        public DateTime startDate;
    }

    [Serializable]
    public class Photo
    {
        public Image Data;
        public String Comment;

        public Photo(Image data, String comment)
        {
            Data = data;
            Comment = comment;
        }
        public override string ToString()
        {
            return Comment;
        }
    }

    /// <summary>
    /// Structure that describes a piece of equipment that can be rented
    /// </summary>
    [Serializable]
    public class Rent
    {
        public Equipment Item;
        public Int32 Price;
        public Rent(Equipment item, Int32 price)
        {
            this.Item = item;
            this.Price = price;
        }
    }

    [Serializable]
    public class PriceSchedule
    {
        [Serializable]
        public struct Timing
        {
            public int start;
            public int end;

            public override string ToString()
            {
                return String.Format("{0}.00 - {1}.00", start, end);
            }
        }

        public List<Timing> Intervals = new List<Timing>();
        public int[][] Table;

    }

    /// <summary>
    /// Describes a piece of equipment.
    /// </summary>
    [Serializable]
    public class Equipment
    {
        public string Name;
        public string Description;
        public String URL;

        public Equipment(String name, String desc, String url)
        {
            this.Name = name;
            this.Description = desc;
            this.URL = url;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class Address
    {
        public String streetName;
        public String buildingNumber;
        public String postfix;

        public String FullAddress
        {
            get { return String.Format("{0} {1} {2}", streetName, postfix, buildingNumber); }
        }

        public override String ToString()
        {
            return FullAddress;
        }
    }

    /// <summary>
    /// Describes the part of studio - room. A studio can consist of one or more rooms with equipment.
    /// </summary>
    [Serializable]
    public class Room
    {
        public string Name { get; set; }

        public List<Equipment> GuitarAmps { get; set; }
        public List<Equipment> BassAmps { get; set; }
        public List<Equipment> OtherMicrophones { get; set; }

        public Equipment Drums { get; set; }
        public DrumParts DrumParts;

        public Room()
        {
            GuitarAmps = new List<Equipment>();
            BassAmps = new List<Equipment>();
            OtherMicrophones = new List<Equipment>();

            Drums = new Equipment("Drums name", "Drums description", "URL");
        }

        public override string ToString()
        {
            return Name;
        }
    }



    [Serializable, DataContract]
    public class Studio : ISerializable
    {

        

        public static Studio FromFile(string filename)         {
            BinaryFormatter binFormatter = new BinaryFormatter();

            object data = null;

            try
            {
                using (FileStream file = File.OpenRead(filename))
                    data = binFormatter.UnsafeDeserialize(file, null);
            }
            catch (SerializationException)
            {
                Log.LogWarning("Incompatible versions of studio (Merging) : " + filename);
            }


            return data as Studio;
        }

        public void SaveToFile(string filename)
        {
            FileStream file = File.Create(filename);
            var BF = new BinaryFormatter();

            
            BF.Serialize(file, this);

            file.Close();
        }

        public static bool CheckKeyEx(string name, int key)
        {
            return true;
        }

        // Key for "trial" is "1635320971" ;) Hello hackers :-p
        public bool CheckKey()
        {
            
            //return studio.Name.GetHashCode() == studio.Key;

            //For debug
            return true;
        }

        public static StudioAttribute[] RegisteredAttributes =
        { 
            new StudioAttribute("Вентиляция", typeof(bool)),
            new StudioAttribute("Подвал/Бомбоубежище", typeof(bool))

            
        }; 

        
        [DataMember]
        public Int32 Key;
        [DataMember]
        public PriceSchedule Schedule { get; set; }
        [DataMember]
        public DateTime Stamp { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public PointF Coords { get; set; }
        [DataMember]
        public string Subway { get; set; }
        [DataMember]
        public string Discrict { get; set; }
        [DataMember]
        public Address Addr { get; set; }
        [DataMember]
        public String Phones { get; set; }

        [DataMember]
        public List<Room> Rooms { get; set; }

        [DataMember]
        public List<Rent> Rents { get; set; }
        [DataMember]
        public List<Photo> Photos { get; set; }

        [DataMember]
        public ReserveTable Reservetable { get; set; }
        [DataMember]
        public string Description { get; set; }

        public Studio()
        {
            Rents = new List<Rent>();
            Addr = new Address();
            Photos = new List<Photo>();
            Rooms = new List<Room>();
        }

        public override string ToString()
        {
            return Name;
        }

        [DataMember]
        public Dictionary<String, object> Attributes;

}
}
